package com.cn.aam.checaiduo.ui.me.adapter;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.cn.aam.checaiduo.AppData;
import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.backend.RetrofitHome;
import com.cn.aam.checaiduo.backend.observer.OnRequestListener;
import com.cn.aam.checaiduo.backend.rx.RxSchedulers;
import com.cn.aam.checaiduo.base.BaseActivity;
import com.cn.aam.checaiduo.base.adapter.BaseRecycerAdapter;
import com.cn.aam.checaiduo.base.adapter.holder.BaseRecyclerViewHolder;
import com.cn.aam.checaiduo.bean.ReservationB;
import com.cn.aam.checaiduo.bean.base.BaseResponseB;
import com.cn.aam.checaiduo.center.CustomDialogCenter;
import com.cn.aam.checaiduo.ui.me.ReservationListActivity;
import com.cn.aam.checaiduo.widget.WMToast;

import butterknife.BindView;

/**
 * monker on 2017/6/14.
 */

public class ReservationListAdapter extends BaseRecycerAdapter<ReservationB> {
    public ReservationListAdapter(Context mContext) {
        super(mContext);
    }

    @Override
    protected BaseRecyclerViewHolder getViewHolder(View picNewsView) {
        return new ReservationHolder(picNewsView);
    }

    @Override
    protected int getItemLayoutRes() {
        return R.layout.list_reservation_item;
    }

    @Override
    protected void ItemVIewChange(BaseRecyclerViewHolder recyclerholder, final int position) {
        ReservationHolder holder = (ReservationHolder) recyclerholder;
        holder.reserTvMount.setText("¥" + getItem(position).getAmount());
        holder.reserTvQueueMount.setText("¥" + getItem(position).getPd_amount());
        holder.reserTvRank.setText("第" + getItem(position).getPd_rank() + "位");
        holder.reserTvDate.setText(getItem(position).getCreate_time());
        holder.reserTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CustomDialogCenter.getInstance().showTipsDialog(mContext, "是否取消预约", "取消", "确定", new CustomDialogCenter.OnTipClick() {
                    @Override
                    public void onLeftButtonClick() {

                    }

                    @Override
                    public void onRightButtonClick() {
                        doCancel(getItem(position).getId());
                    }
                });
            }
        });
    }

    class ReservationHolder extends BaseRecyclerViewHolder {
        @BindView(R.id.reser_tv_mount)
        TextView reserTvMount;
        @BindView(R.id.reser_tv_cancel)
        TextView reserTvCancel;
        @BindView(R.id.reser_tv_queueMount)
        TextView reserTvQueueMount;
        @BindView(R.id.reser_tv_rank)
        TextView reserTvRank;
        @BindView(R.id.reser_tv_date)
        TextView reserTvDate;

        public ReservationHolder(View itemView) {
            super(itemView);
        }
    }

    private void doCancel(String autotender) {
        RetrofitHome.getInstance().getInternalApi().cancelAutoTender(AppData.getToken(), autotender)
                .compose(RxSchedulers.<BaseResponseB>toMain())
                .subscribe(new OnRequestListener<BaseResponseB>(((BaseActivity) mContext)) {
                    @Override
                    public void onNext(BaseResponseB value) {
                        super.onNext(value);
                        WMToast.normal(value.getMsg());
                        if (value.getCode() == 1) {
                            ((ReservationListActivity) mContext).getAutoTenderList();
                        }
                    }
                });
    }

}
